import Vue from 'vue';
import VueRouter from 'vue-router';
import login from '../views/login/index';
import HomeLayout from '../views/HomeLayout.vue'; // 新建的统一布局组件
import register from '../views/register/index';
import notfound from '../views/common/notfound.vue'
Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    name: 'index',
    component: login,
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
  path: '/register',
  name: 'register',
  component: register
},
  // 组织者路由
  {
    path: '/organizer',
    component: HomeLayout,
    meta: { requireAuth: true, role: 'organizer' },
    children: [
      {
        path: 'eventMap',
        name: 'organizerEventMap',
        component: () => import('../views/common/eventMap')
      },
      {
        path: 'history',
        name: 'organizerHistory',
        component: () => import('../views/common/history')
      },
      {
        path: 'eventInfo',
        name: 'organizerEventInfo',
        component: () => import('../views/common/eventInfo')
      },
      {
        path: 'publishEvent',
        name: 'publishEvent',
        component: () => import('../views/organizer/publishEvent')
      },
      {
        path: 'reviewRegistration',
        name: 'reviewRegistration',
        component: () => import('../views/organizer/reviewRegistration')
      },
      {
        path: 'updateInfo',
        name: 'organizerUpdateInfo',
        component: () => import('../components/updateInfo')
      },
      // 默认重定向
      {
        path: '',
        redirect: '/organizer/eventMap'
      }
    ]
  },
  // 队伍路由
  {
    path: '/team',
    component: HomeLayout,
    meta: { requireAuth: true, role: 'team' },
    children: [
      {
        path: 'eventMap',
        name: 'teamEventMap',
        component: () => import('../views/common/eventMap')
      },
      {
        path: 'history',
        name: 'teamHistory',
        component: () => import('../views/common/history')
      },
      {
        path: 'eventInfo',
        name: 'teamEventInfo',
        component: () => import('../views/common/eventInfo')
      },
      {
        path: 'registerEvent',
        name: 'registerEvent',
        component: () => import('../views/team/registerEvent')
      },
      {
        path: 'updateInfo',
        name: 'teamUpdateInfo',
        component: () => import('../components/updateInfo')
      },
      // 默认重定向
      {
        path: '',
        redirect: '/team/eventMap'
      }
    ]
  },
  // 观众路由
  {
    path: '/audience',
    component: HomeLayout,
    meta: { requireAuth: true, role: 'audience' },
    children: [
      {
        path: 'eventMap',
        name: 'audienceEventMap',
        component: () => import('../views/common/eventMap')
      },
      {
        path: 'history',
        name: 'audienceHistory',
        component: () => import('../views/common/history')
      },
      {
        path: 'eventInfo',
        name: 'audienceEventInfo',
        component: () => import('../views/common/eventInfo')
      },
      {
        path: 'updateInfo',
        name: 'audienceUpdateInfo',
        component: () => import('../components/updateInfo')
      },
      // 默认重定向
      {
        path: '',
        redirect: '/audience/eventMap'
      }
    ]
  },
  {
    path: '*',
    name: 'notfound',
    component: notfound
  }
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});

router.beforeEach((to, from, next) => {
  console.log(from.path + ' ====> ' + to.path);
  const isAuthenticated = sessionStorage.getItem("token");
  const userRole = sessionStorage.getItem("type");

  // 检查是否需要认证
  if (to.meta.requireAuth) {
    if (isAuthenticated) {
      // 检查角色权限
      if (to.meta.role && to.meta.role !== userRole) {
        next('/' + userRole); // 重定向到用户角色对应的首页
      } else {
        next();
      }
    } else {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      });
    }
  } else {
    // 已登录用户访问登录页，重定向到对应角色首页
    if (isAuthenticated && to.path === '/login') {
      next('/' + userRole);
    } else {
      next();
    }
  }
});

export default router;